#include <NewSoftSerial.h>
int tx = 7;//PIN 1
int rx = 8;//PIN 2

NewSoftSerial rfid= NewSoftSerial(rx, tx);

void const_rfcomd(char *msg,char *myfc,char *mydata) {
  //RF-221 header
  msg[0]=1;//SOH
  msg[1]='S';//PT
  msg[2]='0';//ID1
  msg[3]='1';//ID2
  msg[4]=myfc[0];
  msg[5]=myfc[1];
  msg[6]=2;//STX
  int i=0;
  int m=7;
  while(mydata[i]!='\0'){
    //Serial.println(mydata[i]);//debug
    msg[m]=mydata[i];
    i++;
    m++;
  }
  msg[m]=3;
  byte bcc=msg[0];
  for(i=1;i<=m;i++){
    bcc=bcc^byte(msg[i]);
  }
  i=20;
  bcc=bcc|B100000;
  msg[m+1]=bcc;
  msg[m+2]='\0';
}

void setup() {
  Serial.begin(9600);
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  rfid.begin(9600);
}

int status=0;
int tmp;
void loop(){
  if(status==0){
    char msg[10];
    const_rfcomd(msg,"E1","\0");//GET Module Version
    //const_rfcomd(msg,"A1","\0");//GET Card ID
    //const_rfcomd(msg,"K0","M00\0");//Read Block 00
    //Serial.print(msg);//debug
    rfid.print(msg);
    delay(100);
    status=1;
  }else{
    //*New Software Serial
    if(rfid.available()>0){
      tmp=rfid.read();
      Serial.print(tmp,HEX);
      //Serial.print(":");
      //Serial.print(tmp,BYTE);
      //Serial.print(",");
    }
    //*/
  }
}
